package com.lyncs.ods.modules.txn.controller;


import com.lyncs.ods.common.api.CommonPage;
import com.lyncs.ods.common.api.CommonResult;
import com.lyncs.ods.modules.txn.service.SettlementService;
import com.lyncs.ods.req.ListPageSearchReq;
import com.lyncs.ods.req.SaveOrderReq;
import com.lyncs.ods.req.SaveSettlementReq;
import com.lyncs.ods.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 结算单详情 前端控制器
 * </p>
 *
 * @author alex
 * @since 2022-02-15
 */
@RestController
@RequestMapping("/txn/settlement")
@Api(tags = {"结算相关接口"})
public class SettlementController {

    @Autowired
    private SettlementService settlementService;

    @PostMapping("save")
    @ApiOperation(value = "创建/修改 结算单")
    public CommonResult<Long> saveSettlement(@Valid @RequestBody SaveSettlementReq req) {
        return CommonResult.success(settlementService.saveSettlement(req));
    }

    @PostMapping("list")
    @ApiOperation(value = "获取 结算单列表")
    public CommonResult<CommonPage<SettlementPageResp>> listSettlement(@RequestBody ListPageSearchReq req) {
        return CommonResult.success(settlementService.listSettlement(req));
    }

    @GetMapping("{settlementId}/detail")
    @ApiOperation(value = "获取 结算单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "settlementId", value = "结算单ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<SettlementDetailInfoResp> getSettlementDetail(@PathVariable Long settlementId) {
        return CommonResult.success(settlementService.getSettlementDetail(settlementId));
    }

    @GetMapping("{settlementId}/top")
    @ApiOperation(value = "置顶/取消置顶")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "settlementId", value = "结算单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "操作类型:0.取消置顶;1.置顶", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<?> stickyOnTop(@PathVariable Long settlementId, @RequestParam Integer type) {
        settlementService.stickyOnTop(settlementId, type);
        return CommonResult.success();
    }

    @GetMapping("{settlementId}/reverse")
    @ApiOperation(value = "撤销结算单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "settlementId", value = "结算单ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<?> reverseSettlement(@PathVariable Long settlementId) {
        settlementService.reverseSettlement(settlementId);
        return CommonResult.success();
    }

    @GetMapping("{settlementId}/status")
    @ApiOperation(value = "结算单状态流转")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "settlementId", value = "结算单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "status", value = "要更改的状态。.已创建（已出账单）；2.结算中（已结算待确认）；3.已确认", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<?> updateStatus(@PathVariable Long settlementId, @RequestParam("status") Integer status) {
        settlementService.updateStatus(settlementId, status);
        return CommonResult.success();
    }

    @GetMapping("listUnsettledDelivery")
    @ApiOperation(value = "获取未完全结算交付单中的款项信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "seller_id", value = "卖方公司id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "buyer_id", value = "买方公司id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "currency", value = "交付单请求币种", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deliver_id", value = "跳转交付单id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "page，默认1", value = "当前页数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "当前页请求数量，默认5", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<CommonPage<UnsettledDeliveryBillResp>> listUnsettledDelivery(@RequestParam("seller_id") Long sellerId,
                                                                                     @RequestParam("buyer_id") Long buyerId,
                                                                                     @RequestParam("currency") String currency,
                                                                                     @RequestParam(value = "deliver_id", required = false) Long deliverId,
                                                                                     @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                                     @RequestParam(value = "page_size", required = false, defaultValue = "5") Integer pageSize) {
        return CommonResult.success(settlementService.listUnsettledDelivery(sellerId, buyerId, currency, deliverId, page, pageSize));
    }

    @GetMapping("{settlementId}/log")
    @ApiOperation(value = "结算单某账款结算历史，请指定settlement_id或bill_id参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "settlementId", value = "结算单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "bill_id", value = "账款 ID，不传查整单", dataType = "Long", paramType = "query"),
    })
    public CommonResult<SettlementBillLogResp> getBillSettleLog(@PathVariable(value = "settlementId") Long settlementId,
                                                                @RequestParam(value = "bill_id", required = false) Long billId) {
        return CommonResult.success(settlementService.getBillSettleLog(billId, settlementId));
    }

    @GetMapping("{settlementId}/editLog")
    @ApiOperation(value = "交付单变更历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "settlementId", value = "结算单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "page", value = "当前页数，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "当前页请求数量，默认20", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<CommonPage<TxnEditLogResp>> getSettlementEditLog(@PathVariable Long settlementId,
                                                                         @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                         @RequestParam(value = "page_size", required = false, defaultValue = "20") Integer pageSize) {
        return CommonResult.success(settlementService.getSettlementEditLog(settlementId, page, pageSize));
    }
}

